package com.qdxwx.data.service.document;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.data.mapper.document.DocumentMapper;
import com.qdxwx.models.document.Document;
import com.qdxwx.models.document.DocumentDetail;
import com.qdxwx.models.document.DocumentQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class DocumentServiceImpl implements DocumentService{

    @Autowired
    private DocumentMapper documentMapper;

    @Override
    public DocumentDetail get(int id) {
        if(id < 1){
            return null;
        }
        return documentMapper.selectById(id);
    }


    @Override
    public Document getLast(long id, DocumentQuery query) {
        if (id <= 0) return null;
        return documentMapper.selectLast(id, query);
    }

    @Override
    public Document getNext(long id, DocumentQuery query) {
        if (id <= 0) return null;
        return documentMapper.selectNext(id, query);
    }

    @Override
    public List<Document> getListTop(DocumentQuery query, int limit) {
        return documentMapper.selectListTop(query, limit);
    }

    @Override
    public List<Document> getList(DocumentQuery query, PageRequest page) {
        return documentMapper.selectList(query, page);
    }

    @Override
    public int count(DocumentQuery query) {
        return documentMapper.selectCount(query);
    }

    @Override
    public int insert(DocumentDetail document) {
        document.setUpdateTime(LocalDateTime.now());
        document.setCreateTime(LocalDateTime.now());
        return documentMapper.insertDocument(document);
    }

    @Override
    public int delete(int id) {
        if(id < 1) return 0;
        return documentMapper.deleteById(id);
    }

    @Override
    public int update(DocumentDetail document) {
        if(document.getId() < 1) return 0;
        document.setUpdateTime(LocalDateTime.now());
        return documentMapper.updateDocument(document);
    }

    @Override
    public int updateView(int id) {
        if(id < 1) return 0;
        return documentMapper.updateView(id);
    }

    @Override
    public int updateDown(int id) {
        if(id < 1) return 0;
        return documentMapper.updateDown(id);
    }

    @Override
    public int updateLike(int id) {
        if(id < 1) return 0;
        return documentMapper.updateLike(id);
    }
}
